package com.mor.shiro.test;

import java.util.*;

class Solution {
    public static boolean isMagic(int[] target) {
        int n = target.length;
        int[] orl = target.clone();
        Arrays.sort(orl);

        for (int k = 1; k <= n; k++) {
            LinkedList<Integer> list = new LinkedList<>();
            LinkedList<Integer> res = new LinkedList<>();
            for (int i = 0; i < orl.length; i++) {
                list.add(orl[i]);
            }
            while (null!=list&&list.size() > 0) {
                Iterator<Integer> iterator = list.iterator();
                    int t = 1;
                    LinkedList<Integer> head = new LinkedList<>();
                    while (iterator.hasNext()) {
                        int num = iterator.next();
                        if (t % 2 == 0) {
                            head.add(num);
                            iterator.remove();
                        }
                        t++;
                    }

                    head.addAll(list);
                    list = head;
                    if(list.size()>=k){
                        for (int i = 1; i <= k; i++) {
                            res.add(list.removeFirst());
                        }
                    }else{
                        res.addAll(list);
                        list = null;
                    }

            }
            if (judge(target, res)) return true;
        }
        return false;
    }

    public static boolean judge(int[] target, List<Integer> res) {
        int i;
        for (i = 0; i < target.length; i++) {
            if (target[i] != res.get(i)) {
                break;
            }
        }
        if (i == target.length) {
            return true;
        }
        return false;
    }


    public static void main(String[] args) {
        int[] array = new int[]{2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,55,59,63,67,53,57,61,65,69};
        System.out.println(isMagic(array));
    }
}